<?

/**
* fetch the header info, TR_DATE
* 
* @param  string    $COracle
* @param  string    $jobId
* @param  string    $srcDb
* @param  string    $resHeader
*
*/
function fetchTxHeader(&$COracle,
                       &$jobId,
                       &$srcDb,
                       &$resHeader){

  $query  = "SELECT TR_DATE
             FROM ".$srcDb." 
             WHERE  JOB_ID='".$jobId."' ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $resHeader);
}

/**
* fetch the detail info
* 
* @param  string    $COracle
* @param  string    $jobId
* @param  string    $idTx
* @param  string    $srcDb
* @param  string    $resDetail
*
*/
function fetchTxDetail(&$COracle,
                       &$jobId,
                       &$idTx,
                       &$srcDb,
                       &$resDetail){

  $query  = "SELECT *
             FROM ".$srcDb." 
             WHERE  JOB_ID='".$jobId."' 
             ORDER BY NO ASC ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $resDetail);
}

/**
* fetch the list of valid transactions
*
* @param  string    $COracle
* @param  string    $condt      the condition
* @param  string    $srcDb
* @param  string    $lstResult
*
*/
function fetchLstTransaction(&$COracle,
                             $condt,
      	                     &$srcDb,
      	                     &$nResult,
      	                     &$lstResult){
      	                      
  $query  = "SELECT JOB_ID,
                    REFERENCE,
                    TR_DATE,
                    STATUS
             FROM ".$srcDb."
             ".$condt." 
             ORDER BY TR_DATE DESC ";
             
  $COracle->execFetchQuery($query,
                           $nResult,
                           $lstResult);
}

/**
* Fetch model id 
* 
* @param  string  $COracle
* @param  string  $model
* @param  string  $modelId
* @param  string  $unit
* @param  string  $srcDb
* @param  string  $retId
*
*/
function fetchId( &$COracle,
                  &$model,
                  &$modelId,
                  &$unit,
                  &$srcDb,
                  &$retId){
                    
  $query  = "SELECT MODEL_ID
             FROM ".$srcDb." 
             WHERE  MODEL='".$model."' 
                    AND UNIT='".$unit."' ";
                    
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $retId = $result["MODEL_ID"][0];
}

/**
* fetch the manual input detail
*
* @param  string  $COracle
* @param  string  $jobId
* @param  string  $srcDb
* @param  string  $manInData
*
*/
function fetchManEntDetail(&$COracle,
                           &$jobId,
                           &$srcDb,
                           &$manInData){

  $query  = "SELECT * 
             FROM ".$srcDb." 
             WHERE  JOB_ID='".$jobId."'
             ORDER BY NO ASC ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  /**
  * we concat all the detail
  *
  */         
  if($nrows > 0){
    
    for($iRow = 0; $iRow < $nrows; $iRow++){
      
      if($iRow == 0){
        
        $tmpData  = $result["SER_NUMBER"][$iRow];
      }else{
        
        $tmpData .= "\n" . $result["SER_NUMBER"][$iRow];
      }
    }
    
    $manInData = $tmpData;
  }else{
    
    $manInData = "";
  } 
}

/**
* fetch all actives model 
*
* @param  string  $COracle
* @param  string  $srcDb
* @param  string  $theListModel
*
*/
function fetchActiveModel(&$COracle,
                          &$srcDb,
                          &$theListModel){

  $query  = "SELECT * 
             FROM ".$srcDb."
             WHERE  STATUS='A' 
             ORDER BY MODEL_ID ASC";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $theListModel = $result["MODEL"];
}

/**
* Fetch inventory balance
*
* @param  string  $COracle
* @param  string  $model
* @param  string  $modelId
* @param  string  $unit
* @param  string  $srcDb
* @param  string  $qty
*
*/
function fetchInvBalance(&$COracle,
                         &$model,
                         &$modelId,
                         &$unit,
                         &$srcDb,
                         &$qty){
  
  if($modelId != "" && $unit != ""){
  
    $query  = "SELECT QTY 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."'
                      AND MODEL_ID=".$modelId."
                      AND UNIT='".$unit."' ";
  }else{
    
    $query  = "SELECT QTY 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."' ";
  }
                      
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $qty  = $result["QTY"][0];
}

/**
* Fetch the manual input detail
*
* @param  string  $COracle
* @param  string  $srcSerNumber
* @param  string  $srcDb
* @param  string  $retManInRows
* @param  string  $retManInDetail
*
*/
function fetchManInDetail( &$COracle,
                           &$srcSerNumber,
                           &$srcDb,
                           &$retManInRows,
                           &$retManInDetail){

  $query  = "SELECT *
             FROM ".$srcDb." 
             WHERE  JOB_ID='".$srcSerNumber."' ";
  
  $COracle->execFetchQuery($query,
                           $retManInRows,
                           $retManInDetail);
}

/**
* Fetch the status model of model history
*
* @param  string  $COracle
* @param  string  $srcSerNumber
* @param  string  $srcDb
* @param  boolean $isModelExists
* @param  integer $statusTx
*
*/
function fetchStatusModel(&$COracle,
                          &$srcSerNumber,
                          &$srcDb,
                          &$isModelExists,
                          &$statusTx){

  $query  = "SELECT *
             FROM ".$srcDb." 
             WHERE  SER_NUMBER='".$srcSerNumber."' ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  if($nrows > 0){
    
    $isModelExists = true;
    
    $statusTx = $result["TX_TYPE"][0];
  }else{
    
    $isModelExists = false;
    
    $statusTx = NULL;
  }
}

/**
* Fetch the complete detail of model with this serNumber
*
* @param  string  $COracle
* @param  string  $srcSerNumber
* @param  string  $srcDb
* @param  integer $nRowsMdlHstDetail
* @param  string  $modelHistoryDetail
*
*/
function fetchModelHistoryDtl(&$COracle,
                              &$srcSerNumber,
                              &$srcDb,
                              &$nRowsMdlHstDetail,
                              &$modelHistoryDetail){

  $query  = "SELECT *
             FROM ".$srcDb." 
             WHERE  SER_NUMBER='".$srcSerNumber."' 
             ORDER BY NO DESC ";

  $COracle->execFetchQuery($query,
                           $nRowsMdlHstDetail,
                           $modelHistoryDetail);
                           
  if($nRowsMdlHstDetail > 0){
    
    /* Do nothing */
  }else{
    
    usrDieJs("Error: Serial number is not exists");
  }
}

/**
* Fetch the source warehouse of this model's serNumber
*
* @param  string  $COracle
* @param  string  $srcRef
* @param  string  $srcDb
* @param  string  $srcDbGudang
*
*/
function fetchSrcWarehouse(&$COracle,
                           &$srcRef,
                           &$srcDb,
                           &$srcDbGudang){

  $query  = "SELECT * 
             FROM ".$srcDb." 
             WHERE  JOB_ID='".$srcRef."' ";
             
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
  
  if($nrows > 0){
    
    $srcDbGudang = "WHS_" . $result["WAREHOUSE"][0];
  }else{
                             
    usrDieJs("Error: invalid/Corrupted source of warehouse");
  }
}

/**
* fetch the active WAREHOUSE
*
* @param  string  $COracle
* @param  string  $srcDb
* @param  string  $nWarehouse
* @param  string  $actWarehouse
*
*/
function fetchActiveWarehouse(&$COracle,
                              &$srcDb,
                              &$nWarehouse,
                              &$srcDbGudang){

  $query  = "SELECT * 
             FROM ".$srcDb."
             WHERE  STATUS='"._ACTIVE."' 
             ORDER BY WHS_CODE ASC";
             
  $COracle->execFetchQuery($query,
                           $nWarehouse,
                           $result);
                           
  $srcDbGudang = $result["WHS_NAME"];
}

/**
* fetch last balance of this model of this warehouse
*
* @param  string  $COracle
* @param  string  $model
* @param  integer $modelId
* @param  string  $unit
* @param  string  $srcDb
* @param  integer $modelBalance
* @param  string  $isProcess
*
*/
function fetchLastBalance(&$COracle,
                          &$model,
                          &$modelId,
                          &$unit,
                          &$srcDb,
                          &$modelBalance,
                          &$isProcess){

  if($modelId != "" && $unit != ""){
  
    $query  = "SELECT * 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."'
                      AND MODEL_ID=".$modelId."
                      AND UNIT='".$unit."' ";
  }else{
    
    $query  = "SELECT * 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."' ";
  }
  
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $modelBalance = $result["QTY"][0];
  
  if($modelBalance > 0){
    
    $isProcess = true;
  }else{
    
    $isProcess = false;
  }
}

/**
* fetch last balance of this model of this warehouse
*
* @param  string  $COracle
* @param  string  $model
* @param  integer $modelId
* @param  string  $unit
* @param  string  $srcDb
* @param  integer $modelBalance
* @param  string  $isProcess
*
*/
function fetchIsModelExists(&$COracle,
                            &$model,
                            &$modelId,
                            &$unit,
                            &$srcDb,
                            &$modelBalance,
                            &$isProcess){

  if($modelId != "" && $unit != ""){
  
    $query  = "SELECT * 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."'
                      AND MODEL_ID=".$modelId."
                      AND UNIT='".$unit."' ";
  }else{
    
    $query  = "SELECT * 
               FROM ".$srcDb." 
               WHERE  MODEL='".$model."' ";
  }
  
  $COracle->execFetchQuery($query,
                           $nrows,
                           $result);
                           
  $modelBalance = $result["QTY"][0];
  
  if($nrows > 0){
    
    $isProcess = true;
  }else{
    
    $isProcess = false;
  }
}
?>